package com.hqyj.service.impl;

import com.hqyj.pojo.Exam;
import com.hqyj.dao.ExamMapper;
import com.hqyj.service.IExamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2023-10-17
 */
@Service
public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IExamService {

    @Resource
    ExamMapper examMapper;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Override
    public HashMap<String, Object> queryExamByName(String examName) {

        HashMap<String, Object> map = new HashMap<String, Object>();
        String key = "exam:"+examName;
        Object obj  = redisTemplate.opsForValue().get(key);

        Exam em = null;
        if(obj==null){
           Exam exam = examMapper.queryExamByName(examName);
           //存入redis
            if(exam!=null){
                redisTemplate.opsForValue().set(key,exam,30, TimeUnit.MILLISECONDS);
            }else{
               //设置 空字符串，并且失效时间要小于 30分钟
                redisTemplate.opsForValue().set(key,"",10,TimeUnit.MINUTES);
            }
            map.put("code",200);
            map.put("data",exam);
        }else{
            //
            em=(Exam)obj;
            map.put("code",200);
            map.put("data",em);
        }

        return map;
    }
}
